package com.ruoyi.project.infoBaseProjectStatist.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.project.infoBaseProjectStatist.domain.InfoBaseProjectStatist;
import com.ruoyi.project.infoBaseProjectStatist.service.IInfoBaseProjectStatistService;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目数据统计Controller
 *
 * @author ruoyi
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/infoBaseProjectStatist/infoBaseProjectStatist")
@Api(tags = "项目数据统计")
public class InfoBaseProjectStatistController extends BaseController {
    @Autowired
    private IInfoBaseProjectStatistService infoBaseProjectStatistService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询项目数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoBaseProjectStatist infoBaseProjectStatist) {
        startPage();
        List<InfoBaseProjectStatist> list = infoBaseProjectStatistService.selectInfoBaseProjectStatistList(infoBaseProjectStatist);
        return getDataTable(list);
    }

    /**
     * 导出项目数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:export')")
    @Log(title = "项目数据统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBaseProjectStatist infoBaseProjectStatist) {
        List<InfoBaseProjectStatist> list = infoBaseProjectStatistService.selectInfoBaseProjectStatistList(infoBaseProjectStatist);
        ExcelUtil<InfoBaseProjectStatist> util = new ExcelUtil<InfoBaseProjectStatist>(InfoBaseProjectStatist.class);
        util.exportExcel(response, list, "项目数据统计数据");
    }

    /**
     * 获取项目数据统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBaseProjectStatistService.selectInfoBaseProjectStatistById(id));
    }

    /**
     * 新增项目数据统计
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:add')")
    @Log(title = "项目数据统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBaseProjectStatist infoBaseProjectStatist) {
        return toAjax(infoBaseProjectStatistService.insertInfoBaseProjectStatist(infoBaseProjectStatist));
    }

    /**
     * 修改项目数据统计
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:edit')")
    @Log(title = "项目数据统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBaseProjectStatist infoBaseProjectStatist) {
        return toAjax(infoBaseProjectStatistService.updateInfoBaseProjectStatist(infoBaseProjectStatist));
    }

    /**
     * 删除项目数据统计
     */
    @PreAuthorize("@ss.hasPermi('infoBaseProjectStatist:infoBaseProjectStatist:remove')")
    @Log(title = "项目数据统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBaseProjectStatistService.deleteInfoBaseProjectStatistByIds(ids));
    }


    /**
     * @param
     * @return
     * @Title: 一级项目名称数据统计
     * @Description: 一级项目名称数据统计
     * @author zhz
     * @date 2022/4/7 10:55
     */
    @PostMapping("getProjectStatistics")
    public TableDataInfo getProjectStatistics(@RequestBody JSONObject jsonObject) {
        startPage();
        return getDataTable(infoBaseProjectStatistService.getProjectStatistics(jsonObject));
    }

    /**
     * @param
     * @return
     * @Title: 一级项目名称数据统计
     * @Description: 一级项目名称数据统计
     * @author zhz
     * @date 2022/4/7 14:33
     */
    @PostMapping("getSecondProjectStatistics")
    public AjaxResult getSecondProjectStatistics(@RequestBody JSONObject jsonObject) {
        Integer parentProjectId= jsonObject.getInteger("parentProjectId");
        Optional.ofNullable(parentProjectId).orElseThrow(() -> new RuntimeException("父级项目id不能为空！"));
        return AjaxResult.success("查询成功！",infoBaseProjectStatistService.getSecondProjectStatistics(parentProjectId));
    }





    /**
     * 递归统计省市区
     */
    /*@GetMapping("getTree")
    @ApiOperation("统计省市区")
    @ApiImplicitParam(name = "id", value = "id", dataType = "int")
    public Map<String, Object> getTree(Long id) {
        Map<String, Object> map = infoBaseProjectStatistService.selectSs(id);
        return map;
    }*/

    /**
     *统计省市区(根据地区统计项目信息)
     */
    @PostMapping("getRegionProject")
    @ApiOperation("统计省市区")
    public AjaxResult getRegionProject(@RequestBody  JSONObject jsonObject) {
        Long id=jsonObject.getLong("id");
        Optional.ofNullable(id).orElseThrow(() -> new RuntimeException("区域id不能为空！"));
        String  startTime=jsonObject.getString("startTime");
        String lastTime=jsonObject.getString("lastTime");
        return AjaxResult.success( infoBaseProjectStatistService.getRegionProject(id,startTime,lastTime));
    }

    /**
     * 查询项目数据统计列表
     */
    @PostMapping("/xmList")
    @ApiOperation("查询项目数据统计列表")
    public AjaxResult list( @RequestBody  JSONObject jsonObject) {
        Long id=jsonObject.getLong("id");
        Optional.ofNullable(id).orElseThrow(() -> new RuntimeException("区域id不能为空！"));
        String  startTime=jsonObject.getString("startTime");
        String lastTime=jsonObject.getString("lastTime");
        return AjaxResult.success(infoBaseProjectStatistService.selectByid(id,startTime,lastTime));
    }


}
